package com.voiceblog.logic;

import java.util.Date;
import java.util.List;

import com.voiceblog.dao.IAlbumDAO;
import com.voiceblog.dao.IBlogDAO;
import com.voiceblog.dao.ICommentDAO;
import com.voiceblog.dao.IFriendDAO;
import com.voiceblog.dao.ILeaveMessageDAO;
import com.voiceblog.dao.IMessageDAO;
import com.voiceblog.dao.IPhotoDAO;
import com.voiceblog.dao.IUsersDAO;
import com.voiceblog.model.Album;
import com.voiceblog.model.Blog;
import com.voiceblog.model.Comment;
import com.voiceblog.model.Users;

public class VoiceBlogImpl implements VoiceBlogFacade {

	private IBlogDAO blogDAO;
	private IUsersDAO usersDAO;
	private IAlbumDAO albumDAO;
	private ICommentDAO commentDAO;
	private IFriendDAO friendDAO;
	private IMessageDAO messageDAO;
	private ILeaveMessageDAO leaveMessageDAO;
	private IPhotoDAO photoDAO;

	// -------------------------------------------------------------------------
	// Setter methods for dependency injection
	// -------------------------------------------------------------------------
	public void setBlogDAO(IBlogDAO blogDAO) {
		this.blogDAO = blogDAO;
	}

	public void setUsersDAO(IUsersDAO usersDAO) {
		this.usersDAO = usersDAO;
	}

	public void setAlbumDAO(IAlbumDAO albumDAO) {
		this.albumDAO = albumDAO;
	}

	public void setCommentDAO(ICommentDAO commentDAO) {
		this.commentDAO = commentDAO;
	}

	public void setFriendDAO(IFriendDAO friendDAO) {
		this.friendDAO = friendDAO;
	}

	public void setMessageDAO(IMessageDAO messageDAO) {
		this.messageDAO = messageDAO;
	}

	public void setLeaveMessageDAO(ILeaveMessageDAO leaveMessageDAO) {
		this.leaveMessageDAO = leaveMessageDAO;
	}

	public void setPhotoDAO(IPhotoDAO photoDAO) {
		this.photoDAO = photoDAO;
	}

	public List findAll() {
		return this.blogDAO.findAll();
	}

	public Blog findById(int id) {

		return this.blogDAO.findById(id);
	}

	public void saveBlog(Blog transientInstance) {
		this.blogDAO.save(transientInstance);

	}

	public List findTopBlogs(final int num) {
		return this.blogDAO.findTopBlogs(num);
	}

	public List findTopUsers(final int num) {
		return this.usersDAO.findTopUsers(num);
	}

	public List listMyAlbums(Users user) {
		System.out.println(user);
		String name = user.getAccount();
		List list = albumDAO.findByUserName(name);
		return list;
	}

	public Users getUserByAccount(String account) {

		return this.usersDAO.findById(account);

	}

	public List findAllUsers() {
		return this.usersDAO.findAll();
	}

	public Users findById(String account) {

		return this.usersDAO.findById(account);
	}

	public List findRecentlyBlogs(int num, Users user) {

		return this.blogDAO.findRecentlyBlogs(num, user);
	}

	public List findByProperty(String account, Object value) {

		return this.blogDAO.findByProperty(account, value);
	}

	public List findByArticleId(int articleId) {

		return this.commentDAO.findByArticleId(articleId);
	}

	public List findRecentlyBlogs(String name) {

		return this.blogDAO.findRecentlyBlogs(name);
	}

	public List findFriend(int num, Users user) {

		return this.friendDAO.findFriend(num, user);
	}

	public List findLeaveMessages(int num, Users user) {

		return this.leaveMessageDAO.findLeaveMessages(num, user);
	}

	public void saveComment(Comment comment) {
		this.commentDAO.save(comment);

	}

	public List findAllComment() {

		return this.commentDAO.findAll();
	}

	public List findCommentByArticleId(int articleId) {

		return this.commentDAO.findCommentByArticleId(articleId);
	}

	public void updateBlog(Blog blog) {
		this.blogDAO.updateBlog(blog);

	}

	public void updateUser(Users user) {
		this.usersDAO.updateUser(user);

	}

	public List findByUser(String account, String password) {

		return this.usersDAO.findByUser(account, password);
	}

	public List findNextBlogs(String name, Blog blog) {

		return this.blogDAO.findNextBlogs(name, blog);
	}

	public List findPreviousBlogs(String name, Blog blog) {

		return this.blogDAO.findPreviousBlogs(name, blog);
	}

	public void deleteBlog(Blog blog) {
		this.blogDAO.deleteBlog(blog);

	}

	public List findBlogByTitle(String title) {
		String hql = "from Blog  blog where blog.articleTitle like" + "'%"
				+ title + "%'";
		return this.blogDAO.findByHql(hql);
	}

	public List findBlogByDate(Date date) {

		return this.blogDAO.findBlogByDate(date);
	}

	public List findUsersByBlogName(String blogName) {
		String hql = "from Users  user where user.blogName like" + "'%"
				+ blogName + "%'";
		return this.usersDAO.findByHql(hql);
	}

	public List findBlogByType(String type) {
		String hql=null;
		if(type.equals("voice")){
			hql = "from Blog  blog where blog.voice is not null";
			
		}else if(type.equals("general")){
			hql = "from Blog  blog where blog.voice is  null";
			return this.blogDAO.findByHql(hql);
		}
		return this.blogDAO.findByHql(hql);
		
	}

	public void editBlog(Blog blog) {
		this.blogDAO.editBlog(blog);
		
	}

	public void deleteUser(Users user) {
		this.usersDAO.deleteUser(user);
		
	}

	public List findAllAlbum() {
		
		return this.albumDAO.findAllAlbum();
	}

	public Album findAlbumById(int id) {
	
		return this.albumDAO.findAlbumById(id);
	}

	public void deleteAlbum(Album album) {
		this.albumDAO.deleteAlbum(album);
		
	}
}
